package com.ezuikit.open;


import android.content.Context;
import android.content.Intent;
import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.CallbackContext;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.ezvizuikit.open.EZUIKit;
import com.videogo.openapi.EZOpenSDK;

import com.ezvizuikit.open.EZUIKit;
import com.ezvizuikit.open.EZUIPlayer;
import com.videogo.exception.BaseException;

import com.videogo.openapi.EZConstants.EZPTZCommand;
import com.videogo.openapi.EZConstants.EZPTZAction;
import com.videogo.openapi.EZConstants;
import android.app.Application;
import com.videogo.openapi.EzvizAPI;
import org.apache.cordova.*;
import android.widget.Toast;


import android.os.Handler;
import android.os.Message;


 
public class EzvizVideo extends CordovaPlugin {
     
      private  Application application;
      private EZOpenSDK sdk;
      private String mAppKey;
      private String mAccessToken; 
      private String pp;
      private String deviceSerial;
      private int cameraNo;
      private PTZcontrol play;
 
    private Context context;
    private static final int HIKVISION_ACTIVIT_CODE = 0;
    @Override
    protected void pluginInitialize() {
        super.pluginInitialize();
        context = cordova.getActivity();
       
    }

   public CallbackContext callbackContext;


   private Handler handler = new Handler() {
    public void handleMessage(Message msg) {
      switch (msg.what) {
      case 1:
        String info = (String) msg.obj;
         System.out.println(info);
          Toast.makeText(context,info, 0).show();
         break;
      case 0:
          Toast.makeText(context, "连接网络失败...", 0).show();
        break;
      }
    }
  };
    

     @Override
    public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
          this.callbackContext = callbackContext;

          deviceSerial=args.getString(0);
         String cameraNo1=args.getString(1);
         cameraNo = Integer.parseInt(cameraNo1);   
          mAppKey  = args.getString(2);
          mAccessToken=args.getString(3);
          this.initSDK();
        
         if (action.equals("start")){

            Intent intent = new Intent();
            intent.setClass(context, EzvizActivity.class);
            intent.putExtra("deviceSerial",deviceSerial);  
            intent.putExtra("cameraNo",cameraNo1); 
            intent.putExtra("mAppKey",mAppKey);  
            intent.putExtra("mAccessToken",mAccessToken);  
            cordova.startActivityForResult( this,intent, HIKVISION_ACTIVIT_CODE);
            
            return true;

         } 
  return true;
    }

     
 
    private void initSDK() {
    
       application=this.cordova.getActivity().getApplication();
       EZOpenSDK.initLib(application,mAppKey, "");
       EZUIKit.setAccessToken(mAccessToken);
   }

 

    // private void coolMethod(String deviceSerial,int i ) {
           
    //     switch (i) {       
    //          case 1:
    //            try {
    //              EZOpenSDK.getInstance().controlPTZ(deviceSerial , cameraNo, EZPTZCommand.EZPTZCommandUp,EZPTZAction.EZPTZActionSTART, 1);
    //             } catch (BaseException e) {
    //                 e.printStackTrace();
    //              }
                
    //             break;
    //         case 2:
    //            try {
    //              EZOpenSDK.getInstance().controlPTZ(deviceSerial  , cameraNo, EZPTZCommand.EZPTZCommandDown,EZPTZAction.EZPTZActionSTART, 1);
    //             } catch (BaseException e) {
    //                 e.printStackTrace();
    //             }
                
    //             break;
    //         case 3:
    //         try {
    //              EZOpenSDK.getInstance().controlPTZ(deviceSerial  , cameraNo, EZPTZCommand.EZPTZCommandLeft,EZPTZAction.EZPTZActionSTART, 1);
    //             } catch (BaseException e) {
    //                 e.printStackTrace();
    //             }
                
    //              break;
    //         case 4:
    //           try {
    //              EZOpenSDK.getInstance().controlPTZ(deviceSerial  , cameraNo, EZPTZCommand.EZPTZCommandRight,EZPTZAction.EZPTZActionSTART, 1);
    //             } catch (BaseException e) {
    //                 e.printStackTrace();
    //             }
               
    //             break;
    //         case 5:
    //          try {
    //              EZOpenSDK.getInstance().controlPTZ(deviceSerial  , cameraNo, EZPTZCommand.EZPTZCommandUp,EZPTZAction.EZPTZActionSTOP, 1);
    //             } catch (BaseException e) {
    //                 e.printStackTrace();
    //             }
                
    //             break;
    //         case 6:
    //          try {
    //              EZOpenSDK.getInstance().controlPTZ(deviceSerial  , cameraNo, EZPTZCommand.EZPTZCommandDown,EZPTZAction.EZPTZActionSTOP, 1);
    //             } catch (BaseException e) {
    //                 e.printStackTrace();
    //             }
                 
    //             break;
    //         case 7:
    //          try {
    //              EZOpenSDK.getInstance().controlPTZ(deviceSerial  , cameraNo, EZPTZCommand.EZPTZCommandLeft,EZPTZAction.EZPTZActionSTOP, 1);
    //             } catch (BaseException e) {
    //                 e.printStackTrace();
    //             }
    //              break;
    //         case 8:
    //          try {
    //              EZOpenSDK.getInstance().controlPTZ(deviceSerial  , cameraNo, EZPTZCommand.EZPTZCommandRight,EZPTZAction.EZPTZActionSTOP, 1);
    //             } catch (BaseException e) {
    //                 e.printStackTrace();
    //             }
               
    //             break;
    //         default:
              
    //            break;
    //                 }
    //    }
 }

 